This rmd. file imports oxygen and voltage data from Google sheet Data is used to plot a oxygen and voltage gradient map of the winogradsky column Exponential and linear models were used to analyze data
#Loading Packages
library(tidyverse)
library(googledrive)
library(googlesheets4)
#library(ggplot2)
library(knitr)
library(minpack.lm)
Warning: package ‘minpack.lm’ was built under R version 4.4.2
library(broom)
library(purrr)
library(lme4)
Warning: package ‘lme4’ was built under R version 4.4.2Loading required package: Matrix
Attaching package: ‘Matrix’
The following objects are masked from ‘package:tidyr’:
expand, pack, unpack
#Importing from google sheet
googlesheets4::gs4_deauth()
CleanData_Oxygen <- googlesheets4::read_sheet("https://docs.google.com/spreadsheets/d/1hNakCyqPsTNRFpf1lGjoBQ-papyZ6xHKT6mcvh3LMb8/edit?pli=1&gid=842906991#gid=842906991", "CleanData_Oxygen") |>
mutate(Sample = as.character(Sample))
✔ Reading from Microcosms.
✔ Range ''CleanData_Oxygen''.
kable(CleanData_Oxygen)
| Sample | Depth_cm | DO_µg_L | temp_C |
|---|---|---|---|
| 1004 | 0.0 | 276.0 | 21.2 |
| 1004 | 6.2 | 276.0 | 21.2 |
| 1004 | 8.4 | 275.0 | 21.2 |
| 1004 | 13.1 | 6.3 | 21.2 |
| 1004 | 22.0 | 3.4 | 21.2 |
| 1003 | 0.0 | 101.0 | 20.0 |
| 1003 | 5.8 | 62.0 | 20.0 |
| 1003 | 8.4 | 52.0 | 20.0 |
| 1003 | 13.1 | 29.0 | 20.0 |
| 1003 | 21.3 | 21.0 | 20.0 |
| 1002 | 0.0 | 80.0 | 19.8 |
| 1002 | 6.6 | 45.0 | 19.8 |
| 1002 | 9.3 | 26.0 | 19.8 |
| 1002 | 15.1 | 21.0 | 19.8 |
| 1002 | 21.9 | 19.0 | 19.8 |
| 1001_C | 0.0 | 190.0 | 19.0 |
| 1001_C | 6.9 | 106.0 | 19.0 |
| 1001_C | 10.8 | 99.0 | 19.0 |
| 1001_C | 16.6 | 57.0 | 19.0 |
| 1001_C | 22.7 | 25.0 | 19.0 |
| 1001_W | 0.0 | 103.0 | 19.0 |
| 1001_W | 6.7 | 34.0 | 19.0 |
| 1001_W | 10.2 | 21.0 | 19.0 |
| 1001_W | 15.2 | 11.0 | 19.0 |
| 1001_W | 22.8 | 8.0 | 19.0 |
googlesheets4::gs4_deauth()
CleanData_Voltage <- googlesheets4::read_sheet("https://docs.google.com/spreadsheets/d/1hNakCyqPsTNRFpf1lGjoBQ-papyZ6xHKT6mcvh3LMb8/edit?pli=1&gid=842906991#gid=842906991", "CleanData_Voltage") |>
mutate(Sample = as.character(Sample))
✔ Reading from Microcosms.
✔ Range ''CleanData_Voltage''.
kable(CleanData_Voltage)
| Sample | Depth_cm | Voltage_Water_10s | Voltage_Water_Max | Voltage_Tap_10s | Voltage_Tap_Max |
|---|---|---|---|---|---|
| 1004 | 0.0 | NA | NA | 121.0 | 121.0 |
| 1004 | 6.2 | 160.2 | 190 | -168.0 | -168.0 |
| 1004 | 8.4 | 74.4 | 79 | 84.0 | 122.0 |
| 1004 | 13.1 | -55.0 | 58 | 43.0 | 51.0 |
| 1004 | 22.0 | -200.0 | 204 | 84.0 | 102.0 |
| 1003 | 0.0 | NA | NA | 74.0 | 300.0 |
| 1003 | 5.8 | 27.0 | 27 | -13.0 | 26.0 |
| 1003 | 8.4 | 59.0 | 83 | 57.0 | 57.0 |
| 1003 | 13.1 | -20.0 | 101 | -94.0 | -136.0 |
| 1003 | 21.3 | -25.0 | -30 | 161.0 | 177.0 |
| 1002 | 0.0 | NA | NA | 51.0 | 199.0 |
| 1002 | 6.6 | -39.0 | -40 | 13.0 | 30.0 |
| 1002 | 9.3 | -4.3 | 30 | 196.0 | 199.0 |
| 1002 | 15.1 | 24.0 | -4 | 16.0 | 25.0 |
| 1002 | 21.9 | 27.0 | 28 | 194.0 | 199.0 |
| 1001_C | 0.0 | NA | NA | 187.0 | 187.0 |
| 1001_C | 6.9 | -10.0 | -22 | 76.0 | 300.0 |
| 1001_C | 10.8 | 25.0 | 60 | -2.8 | -2.8 |
| 1001_C | 16.6 | -6.0 | -23 | -10.5 | -3.0 |
| 1001_C | 22.7 | 6.0 | 48 | 168.0 | 174.0 |
| 1001_W | 0.0 | NA | NA | 187.0 | 187.0 |
| 1001_W | 6.7 | 16.0 | -9 | 13.0 | 170.0 |
| 1001_W | 10.2 | 31.0 | 33 | -48.0 | -48.0 |
| 1001_W | 15.2 | 34.0 | 40 | 24.0 | 24.0 |
| 1001_W | 22.8 | 40.0 | -17 | 72.0 | 72.0 |
Combine O2 & Voltage data by sample & depth
Gradients <- left_join(x = CleanData_Oxygen, y = CleanData_Voltage, by = c("Sample", "Depth_cm"))
#Oxygen Gradient Plot
ggplot(data = CleanData_Oxygen) +
geom_point(aes(y = DO_µg_L, x = Depth_cm)) +
#scale_y_reverse() +
#labs( title= "Oxygen Gradients", caption= "Figure X. Dissolved Oxygen (DO) (µg/L) measured in four Winogradsky colums at 5 depths (cm)")
#+ theme(plot.caption= element_text(size = 11, hjust=0)) +
#geom_path(aes(y = Depth_cm, x = DO_µg_L)) +
facet_grid(cols = vars(Sample)) +
theme_bw()
#+ scale_y_continuous(limits = c(0, 300))
#define exponential decay function for data fitting.
exp_decay <- function(x, i, mu){y = i * exp(mu * x)}
O2_nest <- CleanData_Oxygen |> #alternate forward pipe is %>% loaded with tidyverse
nest(.by = "Sample") |>
mutate(DecayFit = purrr::map(data, ~nlsLM(DO_µg_L ~ exp_decay(x = Depth_cm, i, mu),
data = .x)),
DecayTidy = purrr::map(DecayFit, tidy),
DecayParam = purrr::map(DecayFit, glance),
DecayPredict = purrr::map(DecayFit, augment)
)
Warning: There were 5 warnings in `mutate()`.
The first warning was:
ℹ In argument: `DecayFit = purrr::map(...)`.
Caused by warning in `nlsLM()`:
! No starting values specified for some parameters.
Initializing ‘i’, ‘mu’ to '1.'.
Consider specifying 'start' or using a selfStart model
ℹ Run ]8;;ide:run:dplyr::last_dplyr_warnings()dplyr::last_dplyr_warnings()]8;; to see the 4 remaining warnings.
O2_nest |>
unnest(cols = c(DecayPredict)) |>
ggplot() +
geom_point(aes(x = Depth_cm, y = DO_µg_L)) +
geom_line(aes(x = Depth_cm, y = .fitted)) +
geom_point(aes(x = Depth_cm, y = .resid), colour = "red") +
facet_grid(cols = vars(unlist(Sample))) +
theme_bw()
O2_nest |>
unnest(cols = c(DecayTidy)) |>
select(-c(data, DecayFit, DecayParam, DecayPredict)) |>
select(-c(statistic)) |>
pivot_wider(id_cols = Sample, names_from = term, values_from = c(estimate, std.error, p.value)) |>
kable()
| Sample | estimate_i | estimate_mu | std.error_i | std.error_mu | p.value_i | p.value_mu |
|---|---|---|---|---|---|---|
| 1004 | 326.62312 | -0.0779518 | 101.553082 | 0.0494649 | 0.0487222 | 0.2131346 |
| 1003 | 100.85694 | -0.0833277 | 3.557277 | 0.0059295 | 0.0000963 | 0.0007804 |
| 1002 | 78.61257 | -0.0897172 | 6.441339 | 0.0139884 | 0.0011845 | 0.0076805 |
| 1001_C | 190.11905 | -0.0743515 | 10.391455 | 0.0078457 | 0.0003563 | 0.0024909 |
| 1001_W | 102.49196 | -0.1553412 | 3.251191 | 0.0097456 | 0.0000701 | 0.0005369 |
ggplot(data = Gradients) +
geom_point(aes(x = Depth_cm, y = Voltage_Water_10s)) +
facet_grid(cols = vars(Sample)) +
theme_bw()
Gradients %>%
ggplot() +
geom_point(aes(x = Depth_cm, y = Voltage_Water_10s)) +
geom_smooth(aes( x = Depth_cm, y = Voltage_Water_10s), method = "lm", se=FALSE) +
#facet_wrap(vars((Sample))) +
facet_grid(cols = vars(Sample)) +
theme_bw()
# Fit the linear model grouped by Sample
linear_fits <- Gradients %>%
group_by(Sample) %>%
group_map(~ {
model <- lm(Voltage_Water_10s ~ Depth_cm, data = .x)
# Get the tidy model summary
tidy_model <- tidy(model)
# Add the 'Sample' variable back into the final table display
tidy_model$Sample <- unique(.x$Sample)
# Return the tidy model
return(tidy_model)
}, .keep = TRUE) %>%
bind_rows() %>%
# Filter for 'Depth_cm' coefficient (we are examining Voltage in terms of Depth_cm)
filter(term == "Depth_cm")
# Selecting to include Sample, estimate of slope, std.error, and p.value in table output
linear_fits <- linear_fits %>%
select(Sample, estimate, std.error, p.value)
#Displaying the table and specifying sig digits shown
linear_fits %>%
kable(digits=3)
| Sample | estimate | std.error | p.value |
|---|---|---|---|
| 1001_C | 0.167 | 1.606 | 0.927 |
| 1001_W | 1.309 | 0.461 | 0.105 |
| 1002 | 4.016 | 1.478 | 0.113 |
| 1003 | -4.607 | 2.589 | 0.217 |
| 1004 | -22.088 | 2.833 | 0.016 |
NA